package com.dgut.courses_selecting_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.courses_selecting_sys.entity.DeptNotice;
import com.dgut.courses_selecting_sys.entity.Notice;
import com.dgut.courses_selecting_sys.mapper.DeptNoticeMapper;
import com.dgut.courses_selecting_sys.mapper.NoticeMapper;
import com.dgut.courses_selecting_sys.service.IDeptNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dgut.courses_selecting_sys.utils.RedisUtil;
import com.dgut.courses_selecting_sys.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuec
 * @since 2020-09-10
 */
@Slf4j
@Service
public class DeptNoticeServiceImpl extends ServiceImpl<DeptNoticeMapper, DeptNotice> implements IDeptNoticeService {

    @Autowired
    DeptNoticeMapper deptNoticeMapper;
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    RedisUtil redisUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createData(Notice notice,int deptId){
        noticeMapper.insert(notice);
        String ids[] =notice.getToDeptid().split(",");
        DeptNotice deptNotice=new DeptNotice();
        int noticeId=noticeMapper.selectLastAdd();
        deptNotice.setNoticeId(noticeId);
        log.info(Arrays.toString(ids));
        for (String id:ids) {
            deptNotice.setDeptId(Integer.valueOf(id));
            deptNoticeMapper.insert(deptNotice);
            redisUtil.del("notice_Last_new_ten_by_deptId:::"+id);
        }
    };

}
